package com.dubbo.server.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.server.service.map.QuanZiServiceMapp;
import com.dubbo.server.vo.PageResult;
import com.dubbo.server.vo.QuanZiVo;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.VisitorsVo;
import com.tanhua.common.utils.UserThreadLocal;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("movements")
public class QuanZiController {

    @Reference
    private QuanZiServiceMapp quanZiService;

    /**
     * 查询好友动态
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public PageResult queryPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {

        User user = UserThreadLocal.get();
        PageResult pageResult = quanZiService.queryPublishList(page, pageSize, user);
        System.out.println(pageResult);
        return pageResult;

    }


    /**
     * 发送动态
     *
     * @param textContent
     * @param location
     * @param
     * @return
     */
    @PostMapping
    //改了上传逻辑    注意
    public ResponseEntity<String> savePublish(@RequestParam("textContent") String textContent,
                                              @RequestParam(value = "location", required = false) String location,
                                              @RequestParam(value = "latitude", required = false) String latitude,
                                              @RequestParam(value = "longitude", required = false) String longitude,
                                              @RequestParam(value = "imageContent", required = false) MultipartFile[] file) {


        try {
            // 判断Service层返回的状态码
            // 如果是成功的 我们给前台响应回去  ResponseEntity.ok(result);
            //流集合
            List<byte[]> papersBytes = new ArrayList<>();
            //名字
            List<String> filenameDocument = new ArrayList<>();
            for (MultipartFile multipartFile : file) {
                byte[] bytes = multipartFile.getBytes();
                papersBytes.add(bytes);
                String filename = multipartFile.getOriginalFilename();

                filenameDocument.add(filename);
            }

            User user = UserThreadLocal.get();
            String publishId = quanZiService.savePublish(user, textContent, location, latitude, longitude, papersBytes, filenameDocument);
            if (StrUtil.isNotEmpty(publishId)) {
                return ResponseEntity.ok("成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 项目的真实开发
        // 做到一点  只要是返回 必须要有描述或者状态码
        // HTTP 状态码  自己定义的状态码  APP 状态码  调用的时候哪里出现了问题  我为什么给你返回一个错误的状态码
        // 状态码表  100001  执行成功   100002  xxxx
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 查询推荐动态
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("recommend")
    public PageResult queryRecommendPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        User user = UserThreadLocal.get();
        return quanZiService.queryRecommendPublishList(user, page, pageSize);
    }


    /**
     * 点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String publishId) {
        try {
            User user = UserThreadLocal.get();
            Long likeCount = this.quanZiService.likeComment(user, publishId);
            if (likeCount != null) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 取消点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String publishId) {
        try {
            User user = UserThreadLocal.get();
            Long likeCount = this.quanZiService.disLikeComment(user, publishId);
            if (null != likeCount) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


//喜欢逻辑


    /**
     * 喜欢
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> loveComment(@PathVariable("id") String publishId) {
        try {
            User user = UserThreadLocal.get();
            Long loveCount = this.quanZiService.loveComment(user, publishId);
            if (null != loveCount) {
                return ResponseEntity.ok(loveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消喜欢
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> disLoveComment(@PathVariable("id") String publishId) {
        try {
            User user = UserThreadLocal.get();
            Long loveCount = this.quanZiService.disLoveComment(user, publishId);
            if (null != loveCount) {
                return ResponseEntity.ok(loveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
//

    /**
     * 查询单条动态信息
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<QuanZiVo> queryById(@PathVariable("id") String publishId) {
        try {
            User user = UserThreadLocal.get();
            QuanZiVo movements = this.quanZiService.queryById(user, publishId);
            if (null != movements) {
                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 谁看过我
     *
     * @return
     */
    @GetMapping("visitors")
    public ResponseEntity<List<VisitorsVo>> queryVisitorsList() {
        try {
            User user = UserThreadLocal.get();
            List<VisitorsVo> list = this.quanZiService.queryVisitorsList(user);
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 自己的所有动态
     *
     * @return
     */
    @GetMapping("all")
    public ResponseEntity<PageResult> queryAlbumList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "userId") Long userId) {
        try {
            User user = UserThreadLocal.get();
            PageResult pageResult = this.quanZiService.queryAlbumList(user, userId, page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
